import 'package:aihu_getx_app/app/modules/d/d_member/d_member_vipuser_index/controllers/d_member_vipuser_index_controller.dart';
import 'package:aihu_getx_app/widgets/common_title.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart' as intl;

class VipUserListFilterDrawer extends StatelessWidget {
  const VipUserListFilterDrawer({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.find<DMemberVipuserIndexController>();
    return Drawer(
      child: SafeArea(
        minimum: const EdgeInsets.all(0),
        child: Container(
          padding: const EdgeInsets.all(10),
          decoration: const BoxDecoration(
            color: Colors.white,
          ),
          child: FormBuilder(
            key: controller.searchFormKey,
            child: Obx(
              () => ListView(
                children: [
                  const CommonTitle("会员信息筛选"),
                  const Divider(
                    height: 10,
                  ),
                  FormBuilderTextField(
                    name: 'nickname',
                    initialValue: controller.searchForm.value.nickname ?? '',
                    onChanged: (val) {
                      controller.searchForm.update((searchForm) {
                        searchForm!.nickname = val;
                      });
                      controller.onRefresh();
                    },
                    style: Get.textTheme.bodyMedium,
                    decoration: InputDecoration(
                      labelText: '用户昵称',
                      hintText: '请输入用户昵称',
                      suffixIcon: Offstage(
                        offstage: TextUtil.isEmpty(
                            controller.searchForm.value.nickname),
                        child: IconButton(
                          onPressed: () {
                            controller
                                .searchFormKey.currentState?.fields["nickname"]
                                ?.didChange('');
                            controller.onRefresh();
                          },
                          icon: const Icon(Icons.clear),
                        ),
                      ),
                    ),
                  ),
                  FormBuilderRadioGroup(
                    name: "status",
                    initialValue:
                        controller.searchForm.value.status?.toString() ?? '',
                    options: controller.statusList
                        .map((e) => FormBuilderFieldOption(
                              value: e.value,
                              child: Text(e.label!),
                            ))
                        .toList(),
                    onChanged: (val) {
                      controller.searchForm.update((searchForm) {
                        searchForm!.status =
                            TextUtil.isEmpty(val) ? null : int.parse(val!);
                      });
                    },
                    decoration: InputDecoration(
                      labelText: '状态',
                      suffixIcon: Offstage(
                        offstage: controller.searchForm.value.status == null,
                        child: IconButton(
                          onPressed: () {
                            controller
                                .searchFormKey.currentState?.fields["status"]
                                ?.didChange('');
                            controller.onRefresh();
                          },
                          icon: const Icon(Icons.clear),
                        ),
                      ),
                    ),
                  ),
                  FormBuilderTextField(
                    name: 'mobile',
                    initialValue: controller.searchForm.value.mobile ?? '',
                    onChanged: (val) {
                      controller.searchForm.update((searchForm) {
                        searchForm!.mobile = val;
                      });
                      controller.onRefresh();
                    },
                    style: Get.textTheme.bodyMedium,
                    decoration: InputDecoration(
                      labelText: '手机号',
                      hintText: '请输入手机号',
                      suffixIcon: Offstage(
                        offstage: TextUtil.isEmpty(
                            controller.searchForm.value.mobile),
                        child: IconButton(
                          onPressed: () {
                            controller
                                .searchFormKey.currentState?.fields["mobile"]
                                ?.didChange('');
                            controller.onRefresh();
                          },
                          icon: const Icon(Icons.clear),
                        ),
                      ),
                    ),
                  ),
                  FormBuilderTextField(
                    name: 'registerIp',
                    initialValue: controller.searchForm.value.registerIp ?? '',
                    onChanged: (val) {
                      controller.searchForm.update((searchForm) {
                        searchForm!.registerIp = val;
                      });
                      controller.onRefresh();
                    },
                    style: Get.textTheme.bodyMedium,
                    decoration: InputDecoration(
                      labelText: '注册 IP',
                      hintText: '请输入注册 IP',
                      suffixIcon: Offstage(
                        offstage: TextUtil.isEmpty(
                            controller.searchForm.value.registerIp),
                        child: IconButton(
                          onPressed: () {
                            controller.searchFormKey.currentState
                                ?.fields["registerIp"]
                                ?.didChange('');
                            controller.onRefresh();
                          },
                          icon: const Icon(Icons.clear),
                        ),
                      ),
                    ),
                  ),
                  FormBuilderTextField(
                    name: 'loginIp',
                    initialValue: controller.searchForm.value.loginIp ?? '',
                    onChanged: (val) {
                      controller.searchForm.update((searchForm) {
                        searchForm!.loginIp = val;
                      });
                      controller.onRefresh();
                    },
                    style: Get.textTheme.bodyMedium,
                    decoration: InputDecoration(
                      labelText: '最后登录IP',
                      hintText: '请输入最后登录IP',
                      suffixIcon: Offstage(
                        offstage: TextUtil.isEmpty(
                            controller.searchForm.value.loginIp),
                        child: IconButton(
                          onPressed: () {
                            controller
                                .searchFormKey.currentState?.fields["loginIp"]
                                ?.didChange('');
                            controller.onRefresh();
                          },
                          icon: const Icon(Icons.clear),
                        ),
                      ),
                    ),
                  ),
                  FormBuilderDateRangePicker(
                    name: "loginDate",
                    initialValue: GetUtils.isNullOrBlank(
                            controller.searchForm.value.loginDate)!
                        ? null
                        : DateTimeRange(
                            start: intl.DateFormat('yyyy-MM-dd HH:mm:ss').parse(
                              controller.searchForm.value.loginDate![0],
                            ),
                            end: intl.DateFormat('yyyy-MM-dd HH:mm:ss').parse(
                              controller.searchForm.value.loginDate![1],
                            ),
                          ),
                    firstDate: DateTime(2000),
                    lastDate: DateTime.now(),
                    style: Get.textTheme.bodyMedium,
                    format: intl.DateFormat('yyyy-MM-dd'),
                    locale: const Locale('zh'),
                    onChanged: (val) {
                      controller.searchForm.update((searchForm) {
                        searchForm!.loginDate = val == null
                            ? []
                            : [
                                intl.DateFormat('yyyy-MM-dd 00:00:00')
                                    .format(val.start),
                                intl.DateFormat('yyyy-MM-dd 23:59:59')
                                    .format(val.end)
                              ];
                      });
                      controller.onRefresh();
                    },
                    saveText: "确定",
                    helpText: "选择最后登录时间范围",
                    initialEntryMode: DatePickerEntryMode.calendarOnly,
                    pickerBuilder:
                        (BuildContext builderContext, Widget? child) {
                      return SafeArea(
                        minimum: const EdgeInsets.all(20),
                        child: Theme(
                          data: ThemeData.light().copyWith(
                            primaryColor: Get.theme.primaryColor,
                            colorScheme: ColorScheme.light(
                              primary: Get.theme.primaryColor,
                            ),
                            buttonTheme: const ButtonThemeData(
                              textTheme: ButtonTextTheme.primary,
                            ),
                          ),
                          child: child!,
                        ),
                      );
                    },
                    decoration: InputDecoration(
                      labelText: '最后登录时间',
                      suffixIcon: Offstage(
                        offstage: GetUtils.isNullOrBlank(
                            controller.searchForm.value.loginDate)!,
                        child: IconButton(
                          onPressed: () {
                            controller
                                .searchFormKey.currentState?.fields["loginDate"]
                                ?.didChange(null);
                            controller.onRefresh();
                          },
                          icon: const Icon(Icons.clear),
                        ),
                      ),
                    ),
                  ),
                  FormBuilderDateRangePicker(
                    name: "createTime",
                    initialValue: GetUtils.isNullOrBlank(
                            controller.searchForm.value.createTime)!
                        ? null
                        : DateTimeRange(
                            start: intl.DateFormat('yyyy-MM-dd HH:mm:ss').parse(
                              controller.searchForm.value.createTime![0],
                            ),
                            end: intl.DateFormat('yyyy-MM-dd HH:mm:ss').parse(
                              controller.searchForm.value.createTime![1],
                            ),
                          ),
                    firstDate: DateTime(2000),
                    lastDate: DateTime.now(),
                    style: Get.textTheme.bodyMedium,
                    format: intl.DateFormat('yyyy-MM-dd'),
                    locale: const Locale('zh'),
                    onChanged: (val) {
                      controller.searchForm.update((searchForm) {
                        searchForm!.createTime = val == null
                            ? []
                            : [
                                intl.DateFormat('yyyy-MM-dd 00:00:00')
                                    .format(val.start),
                                intl.DateFormat('yyyy-MM-dd 23:59:59')
                                    .format(val.end)
                              ];
                      });
                      controller.onRefresh();
                    },
                    saveText: "确定",
                    helpText: "选择创建时间范围",
                    initialEntryMode: DatePickerEntryMode.calendarOnly,
                    pickerBuilder:
                        (BuildContext builderContext, Widget? child) {
                      return SafeArea(
                        minimum: const EdgeInsets.all(20),
                        child: Theme(
                          data: ThemeData.light().copyWith(
                            primaryColor: Get.theme.primaryColor,
                            colorScheme: ColorScheme.light(
                              primary: Get.theme.primaryColor,
                            ),
                            buttonTheme: const ButtonThemeData(
                              textTheme: ButtonTextTheme.primary,
                            ),
                          ),
                          child: child!,
                        ),
                      );
                    },
                    decoration: InputDecoration(
                      labelText: '创建时间',
                      suffixIcon: Offstage(
                        offstage: GetUtils.isNullOrBlank(
                            controller.searchForm.value.createTime)!,
                        child: IconButton(
                          onPressed: () {
                            controller.searchFormKey.currentState
                                ?.fields["createTime"]
                                ?.didChange(null);
                            controller.onRefresh();
                          },
                          icon: const Icon(Icons.clear),
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(height: 10),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      ElevatedButton(
                        onPressed: () {
                          controller.onRefresh();
                          Get.back();
                        },
                        child: const Text("筛选"),
                      ),
                      ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Get.theme.secondaryHeaderColor,
                        ),
                        onPressed: () {
                          controller.searchFormKey.currentState?.reset();
                          controller.onRefresh();
                          Get.back();
                        },
                        child: const Text("重置"),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
